package com.vq.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vq.entity.Community;
import com.vq.mapper.CommunityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class CommunityService {
    @Autowired
    CommunityMapper communityMapper;

    @Autowired
    CommunityMemberService communityMemberService;

    public boolean insert(String account, String name) {
        LambdaQueryWrapper<Community> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Community::getName, name);
        Community community = communityMapper.selectOne(lqw);
        if (community!=null) return false;
        int bo = communityMapper.insert(new Community(account, name, 1));
        return bo == 1;
    }

    public boolean deleteById(Integer id, String account) {
        LambdaQueryWrapper<Community> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Community::getId, id).eq(Community::getAccount, account);
        return communityMapper.delete(lqw)==1;
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    public Community selectById(Long id) {
        return communityMapper.selectById(id);
    }
    /**
     *
     */
    public Community[] selectByIds(Long[] ids) {
        Community[] communities = new Community[ids.length];
        for (int i = 0; i < ids.length; i++) {
            communities[i] = this.selectById(ids[i]);
        }
        return communities;
    }
    /**
     * 查询自己创建的社区
     * @param account
     * @return
     */
    public List<Community> selectByMe(String account) {
        LambdaQueryWrapper<Community> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Community::getAccount, account);
        return communityMapper.selectList(lqw);
    }
    /**
     * 查询自己关注的社区
     * @return
     */
    public List<Community> selectByMyFollower() {
        return null;
    }
    /**
     * 搜素动态
     * @return
     */
    public List<Community> searchByName(String name) {
        LambdaQueryWrapper<Community> lqw = new LambdaQueryWrapper<>();
        lqw.like(Community::getName, name);
        List<Community> communityList = communityMapper.selectList(lqw);
        //反转集合
        Collections.reverse(communityList);
        return communityList;
    }
    /**
     * 查询对应名称的社区
     * @param name
     * @return
     */
    public Community selectByName(String name) {
        LambdaQueryWrapper<Community> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Community::getName, name);
        return communityMapper.selectOne(lqw);
    }

    /**
     * 成员加一
     * @param id
     * @return
     */
    public boolean addCommunity(Long id) {
        Community community = communityMapper.selectById(id);
        community.setNumberFollowers(community.getNumberFollowers()+1);
        return communityMapper.updateById(community)==1;
    }
    /**
     * 成员减一
     * @param id
     * @return
     */
    public boolean quitCommunity(Long id) {
        Community community = communityMapper.selectById(id);
        community.setNumberFollowers(community.getNumberFollowers()-1);
        return communityMapper.updateById(community)==1;
    }
}
